package com.yznote.cloudesk.message.service.impl;

import com.yznote.cloudesk.common.exception.YException;
import com.yznote.cloudesk.common.pojo.Friend;
import com.yznote.cloudesk.common.pojo.Message;
import com.yznote.cloudesk.common.pojo.User;
import com.yznote.cloudesk.common.tokenShare.YTokenShare;
import com.yznote.cloudesk.message.mapper.FriendMapper;
import com.yznote.cloudesk.message.mapper.MessageMapper;
import com.yznote.cloudesk.message.service.FriendService;
import com.yznote.cloudesk.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class FriendServiceImpl implements FriendService {
    @Autowired
    YTokenShare yTokenShare;
    @Autowired
    FriendMapper friendMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    MessageMapper messageMapper;


    @Transactional
    @Override
    public void tryInsertFriend(Long friendUserId, String notes) {
        Long userId = yTokenShare.get().getUserId();
        LocalDateTime now = LocalDateTime.now();
        //如果曾经是好友
        Friend friendMessage = friendMapper.getFriendMessage(userId, friendUserId);
        if (friendMessage != null) {
            //第二次发起好友请求
            if (friendMessage.getStatus().equals(0)) {
                friendMessage.setNotes(notes);
                friendMessage.setUpdateBy(userId);
                friendMessage.setUpdateTime(now);
                friendMapper.updateFriendNotes(friendMessage);
            }
            if (friendMessage.getStatus().equals(1)) {
                throw new YException("对方已经是你的好友了", 500);
            }
            Friend friendMessage1 = friendMapper.getFriendMessage(friendUserId,userId);
            if (friendMessage1.getStatus().equals(3)) {
                throw new YException("对方已经将你拉黑，无法添加好友", 500);
            }
        }
        Friend friend = new Friend();
        friend.setUserId(userId);
        friend.setFriendUserId(friendUserId);
        friend.setGroup(null);
        friend.setCreateTime(now);
        friend.setCreateBy(userId);
        friend.setNotes(notes);
        friend.setStatus(0);
        friendMapper.tryInsertFriend(friend);
    }

    @Override
    public List<Friend> queryInsetFriend() {
        Long userId = yTokenShare.get().getUserId();
        List<Friend> friends = friendMapper.queryInsertFriend(userId);
        return friends;
    }

    @Override
    @Transactional
    public void agreedInsetFriend(Long friendUserId) {
        Long userId = yTokenShare.get().getUserId();
        LocalDateTime now = LocalDateTime.now();
        Friend friendMessage = friendMapper.getFriendMessage(userId, friendUserId);
        if (friendMessage != null) {
            if (friendMessage.getStatus().equals(1)) {
                throw new YException("你们已经成为好友", 500);
            }
            friendMessage.setUpdateBy(userId);
            friendMessage.setUpdateTime(now);
            friendMessage.setStatus(1);
            friendMapper.updateFriendStatus(friendMessage);
        } else {
            Friend friend = new Friend();
            friend.setUserId(userId);
            friend.setFriendUserId(friendUserId);
            friend.setCreateBy(userId);
            friend.setCreateTime(now);
            friend.setStatus(1);
            friendMapper.insertFriend(friend);
        }
        //设置对方的好友状态为1
        friendMapper.updateFriendStatus(new Friend(friendUserId, userId, now, 1));
    }

    @Override
    public void disagreedInsetFriend(Long friendUserId) {
        Long userId = yTokenShare.get().getUserId();
        LocalDateTime now = LocalDateTime.now();
        //设置对方的好友状态为4 被拒绝
        friendMapper.updateFriendStatus(new Friend(friendUserId, userId, now, 4));
    }

    @Override
    @Transactional
    public void deleteFriend(Long friendUserId) {
        Long userId = yTokenShare.get().getUserId();
        LocalDateTime now = LocalDateTime.now();
        //改变自己对好友状态
        Friend friend = new Friend(userId, friendUserId, now, 2);
        friendMapper.updateFriendStatus(friend);
        //改变好友对自己状态
        friend.setUserId(friendUserId);
        friend.setFriendUserId(userId);
        friend.setUpdateTime(now);
        friend.setStatus(2);
        friendMapper.updateFriendStatus(friend);
        //todo 聊天记录删除
    }

    @Override
    public void BlackoutFriend(Long friendUserId) {
        Long userId = yTokenShare.get().getUserId();
        LocalDateTime now = LocalDateTime.now();
        //改变自己对好友状态
        Friend friend = new Friend(userId, friendUserId, now, 3);
        friendMapper.updateFriendStatus(friend);
        //改变好友对自己状态
        friend.setUserId(friendUserId);
        friend.setFriendUserId(userId);
        friend.setUpdateTime(now);
        friend.setStatus(2);
        friendMapper.updateFriendStatus(friend);
        //todo 聊天记录删除
    }

    @Override
    public void unBlackoutFriend(Long friendUserId) {
        Long userId = yTokenShare.get().getUserId();
        LocalDateTime now = LocalDateTime.now();
        //改变自己对好友状态为已经删除
        Friend friend = new Friend(userId, friendUserId, now, 2);
        friendMapper.updateFriendStatus(friend);
    }

    @Override
    public List<Friend> getblackoutedFriendList() {
        Long userId = yTokenShare.get().getUserId();
        List<Friend> blackoutedFriendList = friendMapper.getblackoutedFriendList(userId);
        return blackoutedFriendList;
    }

    @Override
    public List<User> searchFriendByName(String friendUserName) {
        return userMapper.getUserByName(friendUserName);
    }

    @Override
    public List<Message> getOfflineMessages(Long friendUserId) {
        Long userId = yTokenShare.get().getUserId();
        // 判断对方对发送方的好友状态
        Friend friendMessage = friendMapper.getFriendMessage(userId, friendUserId);
        if (friendMessage == null){
            throw new YException("此用户不存在",500);
        }
        return messageMapper.getOfflineFriendMessages(userId,friendUserId);
    }
}

